<template>
   <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>货单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
        <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="姓名" prop="userid"></el-table-column>
        <el-table-column label="总售价" prop="sumprice"></el-table-column>
        <el-table-column label="收货地址" prop="address"></el-table-column>
        <el-table-column label="销售时间" prop="salestime"></el-table-column>
        <el-table-column label="订单状态" prop="status">
            <template scope="scope">  
                    <el-tag type="danger" v-if="scope.row.status=='1'" size="mini">待处理</el-tag>
                    <el-tag type="warning" v-if="scope.row.status=='2'" size="mini">正在发货</el-tag>
                    <el-tag type="info" v-if="scope.row.status=='3'" size="mini">已收货</el-tag>                               
                </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 查看按钮 -->
            <el-button type="primary" icon="el-icon-search" size="mini" @click="showEditDialog(scope.row.orderid)">查看</el-button>
            <!-- 删除按钮 -->
            <el-button type="success" icon="el-icon-check" size="mini" @click="outOrder(scope.row)">发货</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" 
      :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize" 
      layout="total, prev, pager, next, jumper" :total="totalnum">
      </el-pagination>
    </el-card>

    <!--form显示订单详情 -->
    <el-dialog title="订单详情" :visible.sync="orderDialogVisible" width="45%">
      <!-- 内容主体区域 -->
      <el-form :inline="true" v-for="item in orderDetailList">
      <el-form-item label="油料名">
        <el-input v-model="item.pname" disabled></el-input>
      </el-form-item> 
      <el-form-item label="销售数量">
        <el-input v-model="item.salenum" disabled></el-input>
      </el-form-item> 
      <el-form-item label="状态">
         <template>  
              <el-tag type="danger" v-if="item.status=='1'" size="mini">待处理</el-tag>
              <el-tag type="warning" v-if="item.status=='2'" size="mini">正在发货</el-tag>
              <el-tag type="info" v-if="item.status=='3'" size="mini">已收货</el-tag>                               
        </template>
      </el-form-item> 
    </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="orderDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="orderDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    
   </div>
</template>

<script>
export default {
    data() {
        return {
          // 获取用户的订单的参数对象
          queryInfo: {
            query: '',
            // 当前的页数
            pagenum: 1,
            // 当前每页显示多少条数据
            pagesize: 5
          },
          orderList: [],
          totalnum: 4,
          //控制添加用户的对话框
          addDialogVisible: false,
          //控制查看详情界面
          orderDialogVisible: false,
          //添加用户的表单数据
          addForm: {
            name: '',
            password: '',
            account: '',
            email: '',
            sex: '',
            mobile: '',
            adress: '',
            remake: '油料用户'
          },
          addFormRules: {
            password: [
              { required: true, message: '请输入密码', trigger: 'blur' },
              {
                min: 6,
                max: 15,
                message: '用户密码的长度在6~15个字符之间',
                trigger: 'blur'
              }
            ]
          },
          //控制修改用户对话框的显示与隐藏
          editDialogVisible: false,
          //订单详情信息
          orderDetailList: [],
          //查询到的用户信息
          editForm: {},
          editFormRules: {
            password: [
              { required: true, message: '请输入密码', trigger: 'blur' },
              {
                min: 6,
                max: 15,
                message: '用户密码的长度在6~15个字符之间',
                trigger: 'blur'
              }
            ]
          }

      }       
    },
    created() {
      this.getOrderList()
    },
    methods: {
        async getOrderList() {
          //const {data: res} = await this.$http.get('sysservice/user/pageUser/'+num+'/'+size+'')
          const {data: res} = await this.$http.get('sysservice/order/findAll',{
            params: this.queryInfo
          })
          //console.log(res)
          if(res.code!=2000) {return this.$message.error('获取用户列表失败！')}
          this.orderList = res.data.rows
          this.totalnum = res.data.total
        },
        // 监听 pagesize 改变的事件
        handleSizeChange(newSize) {
          //console.log(newSize)
          this.queryInfo.pagesize = newSize
          this.getOrderList()
        },
        // 监听 页码值 改变的事件
        handleCurrentChange(newPage) {
          //console.log(newPage)
          this.queryInfo.pagenum = newPage
          this.getOrderList()
        },
        //重置对话框
        addDialogClosed() {
          this.$refs.addFormRef.resetFields()
        },
        addUser() {
          //console.log(this.addForm);
          this.$refs.addFormRef.validate(async valid => {
          if (!valid) return
          // 可以发起添加用户的网络请求
          const { data: res } = await this.$http.post('/sysservice/user/addUser', this.addForm)

          if (res.code !== 2000) {
            this.$message.error('添加用户失败！')
          }

          this.$message.success('添加用户成功！')
          // 隐藏添加用户的对话框
          this.addDialogVisible = false
          // 重新获取用户列表数据
          this.getOrderList()
        })
        },
        //展示订单详情的对话框
        async showEditDialog(id) { 
        const {data: res} = await this.$http.get('/sysservice/orderdetail/getOrderDetail', {
            params: {orderid: id}
            })
        if (res.code !== 2000) {
          return this.$message.error('查询用户信息失败！')
        }
        this.orderDetailList = res.data.item
        //console.log(this.orderDetailList);
        this.orderDialogVisible = true

      },
      //发货
      async outOrder(row) {
        //console.log(row);
        let temp = {
          destination: row.address,
          orderId: row.orderid
        }
        //console.log(temp);
        const {data: res}  = await this.$http.post(`sysservice/dispatch/outOrder`, temp)

        if (res.code !== 2000) {
            return this.$message.error('发货失败！')
          }
        this.$message.success('发货成功！')
      },
      // 监听修改用户对话框的关闭事件
      editDialogClosed() {
        this.$refs.editFormRef.resetFields()
      },
      //修改用户信息并提交
      editUserInfo() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        // 发起修改用户信息的数据请求
        const { data: res } = await this.$http.post(
          '/sysservice/user/updateUser', 
          {
            id: this.editForm.id,
            name: this.editForm.name,
            account: this.editForm.account,
            password: this.editForm.password,
            adress: this.editForm.adress
          })
        if (res.code !== 2000) {
          return this.$message.error('更新用户信息失败！')
        }
        // 关闭对话框
        this.editDialogVisible = false
        // 刷新数据列表
        this.getOrderList()
        // 提示修改成功
        this.$message.success('更新用户信息成功！')
      })
    },
    //根据id删除用户
    async removeUserById(id) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      const { data: res } = await this.$http.delete('/sysservice/user/deleteUser/' + id)

      if (res.code !== 2000) {
        return this.$message.error('删除用户失败！')
      }

      this.$message.success('删除用户成功！')
      this.getOrderList()
    },

    }
}
</script>

<style lang="less" scoped>

</style>
